package com.ml.utils.httpclient;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zhaozongyi@mmears.com
 * @date 2021-06-10
 * @time 12:13
 */
@Slf4j
public class HttpTest {

    public static void main(String[] args) {
        Map<String, String> header = Maps.newHashMap();
        header.put("Accept", "application/json, text/plain, */*");
        header.put("Referer", "https://www.xingtu.cn/ad/creator/market");
        header.put("accept-encoding", "gzip, deflate, br");
        header.put("accept-language", "zh-CN,zh;q=0.9");
        header.put("cache-control", "max-age=0");
        header.put("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"");
        header.put("sec-ch-ua-mobile", "?0");
        header.put("sec-fetch-dest:", "empty");
        header.put("sec-fetch-mode", "same-origin");
        header.put("sec-fetch-site", "same-origin");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36");
        header.put("X-CSRFToken", "PXMgZXWCUAROkjEymjutfGtd71SezzpQ");
        header.put("x-login-source", "1");
//        header.put(":authority", "www.xingtu.cn");
//        header.put(":method", "GET");
//        header.put(":path", "/v/api/demand/author_list/?limit=50&need_detail=true&page=2&platform_source=1&task_category=1&tag=1&order_by=score&disable_replace_keyword=false&is_filter=true");
//        header.put(":scheme", "https");
        header.put("cookie", "csrftoken=PXMgZXWCUAROkjEymjutfGtd71SezzpQ; tt_webid=6971985744907617823; passport_csrf_token_default=c17fa125845d21b2bf469737185b1387; passport_csrf_token=c17fa125845d21b2bf469737185b1387; passport_auth_status_ss=ef1c6c16b9971647305925f9a6632c47%2C98f712ac97ca56557b3d9c4ef0f51b89; sid_guard=a2a17c627748e7b0f929f06e1f69aa45%7C1623311076%7C5183999%7CMon%2C+09-Aug-2021+07%3A44%3A35+GMT; uid_tt=0b32dd436ac87757dd012d35e9daa555; uid_tt_ss=0b32dd436ac87757dd012d35e9daa555; sid_tt=a2a17c627748e7b0f929f06e1f69aa45; sessionid=a2a17c627748e7b0f929f06e1f69aa45; sessionid_ss=a2a17c627748e7b0f929f06e1f69aa45; passport_auth_status=ef1c6c16b9971647305925f9a6632c47%2C98f712ac97ca56557b3d9c4ef0f51b89; star_sessionid=bad71b7d5858ddc962b31eb998cc138d; gftoken=YTJhMTdjNjI3N3wxNjIzMzExMDc3MDl8fDAGBgYGBgY; MONITOR_WEB_ID=21a75e54-e1ff-42f0-828f-6772ea4cd7bb");


        List<String[]> listStr = Lists.newArrayList();

//                                            0     1       2         3     4      5     6          7         8             9          10
        String[] stringstitl = new String[]{"头像", "昵称", "达人等级", "标签", "性别", "地址", "粉丝数", "预期CPM", "预期播放量", "个人作品互动率", "价格"};
        listStr.add(stringstitl);
        for (int i = 1; i <= 225; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String url = "https://www.xingtu.cn/v/api/demand/author_list/?limit=20&need_detail=true&page=" + i + "&platform_source=1&task_category=1&tag=1&order_by=score&disable_replace_keyword=false&is_filter=true";
            String result = HttpPoolUtil.doGet(url, null, header, null, 0, false);
            System.out.println(result);
            JSONObject resultOB = JSONObject.parseObject(result);
            JSONObject dataOB = resultOB.getJSONObject("data");
            if (dataOB == null) {
                continue;
            }
            JSONArray authors = dataOB.getJSONArray("authors");
            for (int j = 0; j < authors.size(); j++) {
                String[] strings = new String[11];
                JSONObject author = authors.getJSONObject(j);
                strings[0] = author.getString("avatar_uri");
                System.out.println("====2=====" + author.getString("nick_name") + "=========");
                strings[1] = author.getString("nick_name");//1
                strings[2] = author.getIntValue("grade") + "";
                strings[3] = unicodeToCn(author.getString("tags").replaceAll("\"]", "")
                        .replace("[", "")
                        .replaceAll("\"", "")
                        .replaceAll("\", \"", "")
                        .replaceAll("\"]", "")
                        .replaceAll(" ", "")
                        .replaceAll(",", "")
                );//1
                strings[4] = author.getIntValue("gender") + "";
                System.out.println("====3=====" + author.getString("city") + "=========");
                strings[5] = author.getString("province") + (StringUtils.isEmpty(author.getString("province")) ? "" : "-") + author.getString("city"); //1
                strings[6] = author.getLong("follower") + "";
                strings[7] = author.getString("expected_cpm");
                strings[8] = author.getString("expected_play_num");
                strings[9] = author.getString("personal_interate_rate");
                String price_infos = author.getJSONArray("price_info").getJSONObject(0).getString("price");
                strings[10] = price_infos;
                listStr.add(strings);
            }
        }


        //写入文件
        for (String[] strings : listStr) {
            for (String string : strings) {
                System.out.print(string == null ? 0 : string);
                System.out.print(",");
            }
            System.out.println();
        }

        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter("D:\\" + "xingtu" + ".csv", true));//追加形式
            //写入文件
            for (String[] strings : listStr) {
                for (String string : strings) {
                    out.write(string == null ? 0 + "" : string);
                    out.write(",");
                }
                out.write("\r\n");
            }

            System.out.println("写入本地 完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } // 把缓存区内容压入文件
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            } // 最后记得关闭文件
        }

    }


    //Unicode转中文方法
    private static String unicodeToCn(String unicode) {
        /** 以 \ u 分割，因为java注释也能识别unicode，因此中间加了一个空格*/
        String[] strs = unicode.split("\\\\u");
        String returnStr = "";
        // 由于unicode字符串以 \ u 开头，因此分割出的第一个字符是""。
        for (int i = 1; i < strs.length; i++) {
            returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
        }
        return returnStr;
    }

    //中文转Unicode
    private static String cnToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        String returnStr = "";
        for (int i = 0; i < chars.length; i++) {
            returnStr += "\\u" + Integer.toString(chars[i], 16);
        }
        return returnStr;
    }
}
